// pages/detail/moreDetail.js
import api from '../../api/api'
import { urlObj } from '../../api/url'
let YY = new Date().getFullYear()
let MM = new Date().getMonth() + 1;
let DD = new Date().getDate()
let currentDate = YY + '-' + (  MM > 9 ? MM : '0' + MM) + '-' + (  DD > 9 ? DD : '0' + DD)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date:currentDate,
    pageOptions:{},
    dataList:[],
    currentPage:1,
    showMore:false,
    loadingText:'...'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      pageOptions:options
    })
  },

 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.handleSearch(1)
  },
  onPullDownRefresh(){
    this.onRefresh()
  },
  //刷新
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      dataList: [],
      currentPage: 1,
      showMore:false,
      loadingText:'...'
    })
    this.handleSearch(1);
  },
  onReachBottom(){
    if(this.data.currentPage > 1 && this.data.loadingText == '没有更多了') {
      return;
    } else {
      let nextPage = this.data.currentPage + 1;
      this.setData({
        currentPage: nextPage,
        showMore:true
      })
      setTimeout(()=>{
        this.handleSearch(this.data.currentPage)
      },500)
    }
  },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value,
      dataList: []
    })
    this.handleSearch(1)
  },
  handleSearch(page){
    const params = {
      limit:10,
      page:page,
      imei:this.data.pageOptions.imei,
      time:this.data.date
    }
    wx.showLoading({
      title: '加载中...',
    })
    api.get(urlObj.getDateData,params).then(res => {
       //隐藏loading 提示框
       wx.hideLoading();
       //隐藏导航条加载动画
       wx.hideNavigationBarLoading();
       //停止下拉刷新
       wx.stopPullDownRefresh();
      if(res.statusCode == '200') {
        let prevData = this.data.dataList;
        if(res.data.items){
          res.data.items.forEach(item => {
            item.createtime = item.createtime.split(' ')[1]
          })
          this.setData({
            dataList: prevData.concat(res.data.items) || [],
            showMore:false,
            loadingText: res.data.items.length > 0 ? '...' : '没有更多了'
          })
          console.log(this.data.dataList)
        }else{
          this.setData({
            deviceList: [],
            showMore:false,
            loadingText: '没有更多了'
          })
        }
      } else {
        wx.showToast({
          icon:'none',
          title: res.msg,
        })
      }
    })
  }
})